package com.xiyu.service.repository.setting.business;

import com.xiyu.service.model.setting.business.*;
import com.xiyu.service.vo.setting.businessField.BusinessFieldListQueryInput;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.springframework.util.StringUtils;
import java.util.Optional;
import java.util.List;

public interface SettingBusinessFieldRepository extends JRepository<SettingBusinessField, Long> {
    SettingBusinessFieldTable settingBusinessFieldTable = SettingBusinessFieldTable.$;

    default List<SettingBusinessField> listQuery(BusinessFieldListQueryInput inputVO){
        return sql().createQuery(settingBusinessFieldTable)
                .whereIf(StringUtils.hasText(inputVO.getShowName()), () -> settingBusinessFieldTable.showName().like(inputVO.getShowName()))
                .whereIf(inputVO.getBusinessModule() != null, settingBusinessFieldTable.businessModule().eq(inputVO.getBusinessModule()))
                .orderBy(settingBusinessFieldTable.id())
                .select(settingBusinessFieldTable).execute();
    }

    default Optional<SettingBusinessField> findDetailById(Long id){
        return sql().createQuery(settingBusinessFieldTable)
                .where(settingBusinessFieldTable.id().eq(id))
                .select(settingBusinessFieldTable.fetch(SettingBusinessFieldFetcher.$.allTableFields().options(SettingBusinessFieldOptionFetcher.$.allTableFields())))
                .fetchOptional();
    }

    default List<SettingBusinessField> findByModule(Integer module){
        return sql().createQuery(settingBusinessFieldTable)
                .where(settingBusinessFieldTable.businessModule().eq(module))
                .select(settingBusinessFieldTable.fetch(SettingBusinessFieldFetcher.$.allTableFields().options(SettingBusinessFieldOptionFetcher.$.allTableFields())))
                .execute();
    }

    Optional<SettingBusinessField> findByBusinessModuleAndFieldName(Integer businessModule, String fieldName);


}